home *** CD-ROM | disk | FTP | other *** search
- Path: newsroom.hitc.com!kfreeman
- From: kfreeman@mandrake.HITC.COM (Keith Freeman)
- Newsgroups: comp.lang.c++
- Subject: Re: Help: Containers for templated classes
- Date: 19 Mar 1996 18:12:07 GMT
- Organization: Hughes Team (EOSDIS)
- Message-ID: <KFREEMAN.96Mar19131207@mandrake.HITC.COM>
- References: <314740D4.23B1@mit.edu>
- NNTP-Posting-Host: mandrake.hitc.com
- In-reply-to: Imran Haq's message of Wed, 13 Mar 1996 17:40:36 -0400
- To: ihaq@mit.edu
-
- In article <314740D4.23B1@mit.edu> Imran Haq <ihaq@mit.edu> writes:
-
- > I'm kind of new to advanced usage of templates. Does anyone know how
- > to put different template classes in a single container. Is this possible?
- >
- > myClass<int> *type1;
- > myClass<double> *type;
- >
- > vector<myClass<????> > container;
-
- Simple: make all myClass instantiations inherit from a single base
- class. It can be as simple as you like, e.g.:
-
- class myBase
- {
- virtual void StreamMe(ostream&os) = 0;
- };
- template <class T> class myClass : public myBase
- {
- ...
- };
-
- then,
-
- vector<myClass<myBase> > container;
-
- It should be pretty easy to decide what common functionality (if any)
- you can put in myBase, based on what you want to do with the objects
- contained in container. Any function you want to call using an object
- from the container must have a signature that's template-variable
- independent, and hence can be put in myBase (e.g. StreamMe).
-
- keith freeman
- kfreeman@eos.hitc.com
-
-
-